package com.banciyuan.bcywebview.biz.main.message.messagedetail;

import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.utils.regex.MyLinkfy;
import com.banciyuan.bcywebview.utils.regex.MyRegex;
import com.banciyuan.bcywebview.utils.string.StringUtil;
import com.banciyuan.bcywebview.utils.string.UnderLineUtil;

import java.util.List;

import de.greenrobot.daoexample.model.MessageBroadCast;

/**
 * Created by squall on 2016/4/1.
 */
public class MessageBroadCastAdapter extends BaseAdapter {

    private List<MessageBroadCast> messageList;
    private Context mContext;

    public MessageBroadCastAdapter(Context mContext, List<MessageBroadCast> messageList) {
        this.mContext = mContext;
        this.messageList = messageList;
    }

    @Override
    public int getCount() {
        return messageList.size();
    }

    @Override
    public Object getItem(int position) {
        return messageList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private class ViewHolder {
        TextView time;
        TextView title;
        TextView content;

        public ViewHolder(View convertView) {
            time = (TextView) convertView.findViewById(R.id.time_tv);
            title = (TextView) convertView.findViewById(R.id.title_tv);
            content = (TextView) convertView.findViewById(R.id.content_tv);
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.message_broadcast_item, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        MessageBroadCast m = messageList.get(position);
        if (!TextUtils.isEmpty(m.getCtime()))
            viewHolder.time.setText(StringUtil.getOppositeTime(m.getCtime()));
        if (!TextUtils.isEmpty(m.getTitle()))
            viewHolder.title.setText(Html.fromHtml(m.getTitle()));
        if (!TextUtils.isEmpty(m.getContent())) {
            viewHolder.content.setText(m.getContent());
            MyLinkfy.addLinks(viewHolder.content, MyRegex.plist.get(1), "bcyapp_");
            UnderLineUtil.DisUnderLine(mContext, viewHolder.content);
        }

        return convertView;
    }
}
